package edu.cmu.vlis.wassup.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailClient {
	
	public MailClient(){
	}
	public void sendMail(String to,String subject, String messageBody) throws 
			MessagingException, AddressException 
	{ 
		// Setup mail server 
		
		String mailServer = System.getProperty("mail.smtp.host"); 
		System.out.println(mailServer);

		// Get a mail session 
		Session session = Session.getDefaultInstance(System.getProperties(), null); 

		// Define a new mail message 
		Message message = new MimeMessage(session); 
		message.setFrom(new InternetAddress(System.getProperty("mail.smtp.user"))); 
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
		message.setSubject(subject); 

		// Create a message part to represent the body text 
		BodyPart messageBodyPart = new MimeBodyPart(); 
		messageBodyPart.setText(messageBody); 

		//use a MimeMultipart as we need to handle the file attachments 
		Multipart multipart = new MimeMultipart(); 

		//add the message body to the mime message 
		multipart.addBodyPart(messageBodyPart); 

		// add any file attachments to the message 
		// addAtachments(attachments, multipart); 

		// Put all message parts in the message 
		message.setContent(multipart); 

		// Send the message 
		Transport transport = session.getTransport("smtp");
        System.out.println(System.getProperty("mail.smtp.user"));
        System.out.println(System.getProperty("mail.smtp.password"));
		transport.connect(mailServer, System.getProperty("mail.smtp.user"),System.getProperty("mail.smtp.password"));
		transport.sendMessage(message,message.getAllRecipients()); 
		transport.close();
		} 

	protected void addAtachments(String[] attachments, Multipart multipart) 
	throws MessagingException, AddressException 
	{ 
		for(int i = 0; i<= attachments.length -1; i++) 
		{ 
			String filename = attachments[i]; 
			MimeBodyPart attachmentBodyPart = new MimeBodyPart(); 

			//use a JAF FileDataSource as it does MIME type detection 
			DataSource source = new FileDataSource(filename); 
			attachmentBodyPart.setDataHandler(new DataHandler(source)); 

			//assume that the filename you want to send is the same as the 
			//actual file name - could alter this to remove the file path 
			attachmentBodyPart.setFileName(filename); 

			//add the attachment 
			multipart.addBodyPart(attachmentBodyPart); 
		}
	}
}
